/*
 * @Autor: dingxiaolin
 * @Date: 2021-01-30 21:52:04
 * @LastEditors: dingxiaolin
 * @LastEditTime: 2021-01-30 21:53:41
 */
var COS = require("cos-wx-sdk-v5");
const cos = new COS({
    SecretId: "AKIDCEAD1ZQHoCPRDkoA0cx79LHwLVWKnIm2",
    SecretKey: "ArIWU8WfnmZBaq6B4i7TJ0SeaFfohOp0",
});

// 上传文件
const uploadFile = (filePath, callback) => {
    console.log("filePath", filePath);
    let filename = filePath.substr(filePath.lastIndexOf("/") + 1);
    cos.postObject(
        {
            Bucket: "common-1251807501", //可在腾讯云对象存储中自己查看创建的
            Region: "ap-nanjing",
            Key: filename,
            FilePath: filePath,
            onProgress: (info) => {
                console.log(JSON.stringify(info));
            },
        },
        (err, data) => {
            if (err === null) {
                callback(data);
            } else {
                wx.hideLoading();
                wx.showToast({
                    title: "上传失败",
                    icon: "none",
                });
            }
        }
    );
};

module.exports = {
    uploadFile,
};
//how to use?
// wx.chooseImage({
//     count: 1, // 默认9
//     sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
//     sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
//     success: function (res) {
//         var file = res.tempFiles[0];
//         uploadFile(file.path, (res) => {
//             console.log("666", res.Location);
//             i == 1 && (that.form.photoUrl = "https://"+res.Location);
//             i == 2 && (that.form.photoUrlTwo ="https://"+ res.Location);
//         });
//     }
// })
